{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# for循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Michal\n",
      "Bob\n",
      "Tracy\n"
     ]
    }
   ],
   "source": [
    "# 打印列表的每一个元素\n",
    "names = ['Michal','Bob','Tracy']\n",
    "for name in names:\n",
    "    print(name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "55\n"
     ]
    }
   ],
   "source": [
    "# 计算1-10的整数之和\n",
    "sum = 0\n",
    "for i in range(1,11):\n",
    "    sum += i\n",
    "print(sum)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5050\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[0, 1, 2, 3, 4]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算1-100的整数之和\n",
    "sum = 0\n",
    "for i in range(101):\n",
    "    sum += i\n",
    "print(sum)\n",
    "list(range(5))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# while循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2500\n"
     ]
    }
   ],
   "source": [
    "# 计算100以内所有奇数之和\n",
    "# 方法1：\n",
    "sum = 0\n",
    "n = 1\n",
    "while n < 100:\n",
    "    sum += n\n",
    "    n = n + 2\n",
    "print(sum)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2500\n"
     ]
    }
   ],
   "source": [
    "# 方法2：\n",
    "sum = 0\n",
    "for i in range(101):\n",
    "    if  i % 2 == 1:\n",
    "        sum =sum + i\n",
    "print(sum)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello,Bart\n",
      "Hello,Lisa\n",
      "Hello,Adam\n"
     ]
    }
   ],
   "source": [
    "# 练习\n",
    "# 请利用循环依次对list中的每个名字打印出Hello, xxx!：\n",
    "L = ['Bart', 'Lisa', 'Adam']\n",
    "for i in L:\n",
    "    print(\"Hello,{0}\" .format(i))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# break，continue，pass\n",
    "- break的作用是提前结束循环\n",
    "- continue的作用是提前介绍本轮循环，并直接开始下一轮的循环\n",
    "- pass表示略过，通常用于站位"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n",
      "10\n",
      "END\n"
     ]
    }
   ],
   "source": [
    "# break\n",
    "n = 1\n",
    "while n <= 100:\n",
    "    if n > 10: # 当n = 11时，条件满足，执行break语句\n",
    "        break # break语句会结束当前循环\n",
    "    print(n)\n",
    "    n = n + 1\n",
    "print('END')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "3\n",
      "5\n",
      "7\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "# continue\n",
    "n = 0\n",
    "while n < 10:\n",
    "    n = n + 1\n",
    "    if n % 2 == 0: # 如果n是偶数，执行continue语句\n",
    "        continue # continue语句会直接继续下一轮循环，后续的print()语句不会执行\n",
    "    print(n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 死循环\n",
    "# ？？？\n",
    "sum = 0\n",
    "n = 1\n",
    "while n > 0:\n",
    "    sum += n\n",
    "    n = n + 2\n",
    "print(sum)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
